package com.frogs.ucenter.service.daoimpl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.base.dao.BaseDao;
import com.frogs.framework.base.service.daoimpl.BaseServiceImpl;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.date.DateUtil;
import com.frogs.system.util.UserAccountInfoStatusEnum;
import com.frogs.system.util.UserTypeEnum;
import com.frogs.ucenter.dao.UserAccountInfoDao;
import com.frogs.ucenter.dao.UserLevelBufferRecordDao;
import com.frogs.ucenter.dao.UserLevelInfoDao;
import com.frogs.ucenter.entity.UserAccountInfo;
import com.frogs.ucenter.entity.UserLevelBufferRecord;
import com.frogs.ucenter.entity.UserLevelInfo;
import com.frogs.ucenter.service.UserLevelBufferRecordService;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service("userLevelBufferRecordService")
public class UserLevelBufferRecordServiceImpl extends BaseServiceImpl<UserLevelBufferRecord> implements UserLevelBufferRecordService {
    @Autowired
    private UserLevelBufferRecordDao userLevelBufferRecordDao;
    @Resource
    private UserLevelInfoDao userLevelInfoDao;
    @Resource
    private UserAccountInfoDao userAccountInfoDao;

    public UserLevelBufferRecordServiceImpl() {
        super();
    }

    public UserLevelBufferRecordDao getUserLevelBufferRecordDao() {
        return userLevelBufferRecordDao;
    }

    /**
     * Don't remove or edit it.
     */
    @Override
    protected BaseDao<UserLevelBufferRecord> getBaseDao() {
        return userLevelBufferRecordDao;
    }

    public void setUserLevelBufferRecordDao(UserLevelBufferRecordDao userLevelBufferRecordDao) {
        this.userLevelBufferRecordDao = userLevelBufferRecordDao;
    }
    
    @Override
    public void scanBufferRecord() {
    	Map<String, Object> params = new HashMap<String, Object>();
    	Date time = DateUtil.getDateAfterDays(new Date(), -7);
    	params.put("ltUnBindTime", time);
    	PageList<UserLevelBufferRecord> list = userLevelBufferRecordDao.selectByMap(params, false);
    	for (UserLevelBufferRecord userLevelBufferRecord : list) {
			Integer userId = userLevelBufferRecord.getUserId();
			UserAccountInfo userAccountInfo = userAccountInfoDao.selectById(userId);
			UserLevelInfo userLevelInfo = userLevelInfoDao.selectById(userId);
			if (userAccountInfo.getIsEmailValid() == UserAccountInfoStatusEnum.AVAILABILITY_NO.getId()
					&& userAccountInfo.getIsPhoneValid() == UserAccountInfoStatusEnum.AVAILABILITY_NO.getId()) {
				userLevelInfo.setLevelId(UserTypeEnum.GENERAL.getType());
				userLevelInfoDao.update(userLevelInfo);
				// 调用站内短信 告知已降级
			}
			userLevelBufferRecordDao.deleteById(userLevelBufferRecord.getId());
		}
    }
}